package com.company.BinarySortTree;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}19:49
 */
public class VALTree {
    public static void main(String[] args) {
//        int[] arr = {4,3,6,5,7,8};
//        int[] arr = {10,12,8,9,7,6};
        int[] arr = {10,11,7,6,8,9};
//        int[] arr = {1,2,3,4,5,6};
        AVLTree avlTree = new AVLTree();
        for (int i = 0; i < arr.length;i++){
            avlTree.add(new Node(arr[i]));
        }

        //遍历
        System.out.println("中序");
        avlTree.infixOrder();

        //在没做平衡处理前
        System.out.println("树的高度"+avlTree.getRoot().height());
        System.out.println("左边高度"+avlTree.getRoot().leftHeight());
        System.out.println("右边高度"+avlTree.getRoot().rightHeight());
        avlTree.infixOrder();
    }

    static class AVLTree{
        private Node root;

        public Node getRoot() {
            return root;
        }

        public void setRoot(Node root) {
            this.root = root;
        }

        public void add(Node node){
            if (root == null){
                root = node;
            }else {
                root.add(node);
            }
        }
        //中序遍历
        public void infixOrder(){
            if (root == null){
                System.out.println("树为空");
            }else {
                root.infixOrder();
            }
        }
    }

    static class Node{
        int value;
        Node left;
        Node right;

        public Node(int value) {
            this.value = value;
        }

        //返回左子树的高度
        public int leftHeight(){
            if (left == null){
                return 0;
            }
            return left.height();
        }
        //返回右子树的高度
        public int rightHeight(){
            if (right == null){
                return 0;
            }
            return right.height();
        }

        //返回以该节点为根节点的高度
        public int height(){
            return Math.max(left == null ? 0:left.height(),right == null ? 0:right.height())+1;
        }
        //左旋转
        private void leftRotate(){
            Node newNode = new Node(value);
            newNode.left = left;//把新的节点的左子树设置成当前节点的左子树
            newNode.right = right.left;//把新的节点的右子树设置成带过去节点的右子树的左子树
            value = right.value;//把当前节点的值替换成右子节点的值
            right = right.right;//把当前节点的右子树设置成当前节点右子树的右子树
            left = newNode;//把当前节点的左子树设置成新的节点
        }

        private void rightRotate(){
            Node newNode = new Node(value);
            newNode.right = right;
            newNode.left = left.right;
            value = left.value;
            left = left.left;
            right = newNode;
        }

        //添加节点方法
        //递归的形式添加节点，注意需要满足二叉排序树的要求
        public void add(Node node){
            if (node == null){
                return;
            }

            //判断传入的节点的值，和当前树的根节点的值的关系
            if (node.value < this.value) {
                if (this.left == null) {
                    this.left = node;
                } else {
                    this.left.add(node);
                }
            }else {
                if (this.right == null){
                    this.right = node;
                }else {
                    this.right.add(node);
                }
            }

            //
            //当添加问一个节点后，如果右子树的高度比左子树的高度大于1，左旋转
            if (rightHeight()-leftHeight()>1){
                if (right!=null&&right.leftHeight()>right.rightHeight()){
                    right.rightRotate();
                    leftRotate();
                }else {
                    leftRotate();
                }
                return;
            }

            if (leftHeight() - rightHeight() > 1){
                if (left!=null&&left.rightHeight()>left.leftHeight()){
                    left.leftRotate();
                    rightRotate();
                }else {
                    rightRotate();
                }
            }
        }
        public void infixOrder(){
            if (this.left!=null){
                this.left.infixOrder();
            }
            System.out.println(this);
            if (this.right!=null){
                this.right.infixOrder();
            }
        }

        @Override
        public String toString() {
            return "Node{" +
                    "value=" + value +
                    '}';
        }
    }
}
